package com.sskj.xgja.mine;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.allen.library.SuperTextView;
import com.sskj.base.utils.APKVersionCodeUtils;
import com.sskj.xgja.AppManager;
import com.sskj.xgja.AppUpDateFragment;
import com.sskj.xgja.MVPActivity;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.BuildConfig;
import com.sskj.xgja.R;
import com.sskj.xgja.login.LoginActivity;
import com.sskj.xgja.mine.data.AppVersionBean;

import butterknife.BindView;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;

/**
 * 设置
 *
 * @author Hey
 * Create at  2019/03/20
 */
public class SettingActivity extends MVPActivity<SettingActivityPresenter> {
    @BindView(R.id.set_toolBar)
    ToolBarLayout setToolBar;
    @BindView(R.id.set_login_password)
    SuperTextView setLoginPassword;
    @BindView(R.id.set_version)
    SuperTextView setVersion;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_activity;
    }

    @Override
    public SettingActivityPresenter getPresenter() {
        return new SettingActivityPresenter();
    }

    @Override
    public void initView() {
        setVersion.setRightString("V" + BuildConfig.VERSION_NAME);
    }

    @Override
    public void initData() {
        ClickUtil.click(setLoginPassword, view -> {
            if (isLogin) {
                ResetLoginPasswordActivity.start(this);
            } else {
                LoginActivity.start(this);
            }
        });
        ClickUtil.click(setVersion, view -> mPresenter.checkVersion(BuildConfig.VERSION_NAME));
        setToolBar.setLeftButtonOnClickListener(v -> finish());
    }

    public void checkVersion(AppVersionBean data) {
        if (data == null) {
            return;
        }
        if (!this.getClass().getName().equals(AppManager.getInstance().getCurrentActivity().getClass().getName())) {
            return;
        }
        String verName = APKVersionCodeUtils.getVerName(this);
        data.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);
        if (APKVersionCodeUtils.compareVersion(data.getAppVersion(), verName) == 1) {
            android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            getFragmentManager().beginTransaction();
            AppUpDateFragment appUpDateFragment = new AppUpDateFragment();//自定义UI
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, data);
            appUpDateFragment.setArguments(bundle);
            appUpDateFragment.show(fragmentTransaction, "update");
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

}
